package com.example.controller;


import com.aliyuncs.exceptions.ClientException;
import com.example.common.annotation.OperLog;
import com.example.common.annotation.RepeatSubmit;
import com.example.common.enums.LogType;
import com.example.common.utils.R;
import com.example.system.entity.Files;
import com.example.system.service.IFileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-08-08
 */
@Api(tags = "文件信息API",description="提供文件信息相关的 REST API")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService iFileService;

    @OperLog(operation = "文件管理",type = LogType.ADD)
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public R upload(@RequestParam MultipartFile file) throws IOException, ClientException {
        return iFileService.uploadOSS(file);
    }


    @RequestMapping(value = "/{fileUUID}", method = RequestMethod.GET)
    public void download(@PathVariable("fileUUID") String fileUUID, HttpServletResponse response) throws IOException {
        iFileService.downloadByOSS(fileUUID, response);
    }

    @RequestMapping(value="/selectAll",method=RequestMethod.GET)
    @ResponseBody
    public R selectAll(@RequestParam int currentPage, @RequestParam int pageSize, @RequestParam(defaultValue = "") String name){
        return iFileService.selectAll(currentPage,pageSize,name);
    }

    @OperLog(operation = "文件管理",type = LogType.DELETE)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public R deleteById(@PathVariable("id") Integer id) {
        return iFileService.deleteById(id);
    }

    @OperLog(operation = "文件管理",type = LogType.BATCH_DELETE)
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public R deleteBatchByIds(@RequestBody Integer[] ids) {
        return iFileService.deleteBatchByIds(ids);
    }

    @OperLog(operation = "文件管理",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public R update(@RequestBody Files file){
        return iFileService.update(file);
    }
}

